package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.TicketEntity;

/**
 * Created by qiuzhenhuan on 2019/9/25.
 */

public class ADA_ActivityList extends CommonAdapter<TicketEntity> {

    private List<TicketEntity> mSelectedItemList;

    private boolean isDoubleCheck = false;

    public List<TicketEntity> getSelectedItemList() {
        return ListUtil.deepCopy(mSelectedItemList);
    }

    public ADA_ActivityList(Context context) {
        super(context);
        mSelectedItemList = new ArrayList<>();
    }

    private boolean isEnableSelected;

    public void setEnableSelected(boolean enableSelected) {
        isEnableSelected = enableSelected;
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_activity;
    }

    @Override
    protected void convert(ViewHolder holder, final TicketEntity item, final int position) {

        holder.setChecked(R.id.rb_check, checkTicketIsSelected(item) != null);
        holder.setVisible(R.id.tv_vip_tag, StringUtils.isTrue(StringUtils.get(item.getIsMember(), "0")));
        holder.setText(R.id.tv_activity_name, StringUtils.get(item.getTicketName()));
        holder.setText(R.id.tv_activity_price, "-" + StringUtils.getFormattedMoneyWithUnit(item.getTicketAmount()));

        // 设置条目点击事件
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isEnableSelected)
                    return;

                TicketEntity listBean = checkTicketIsSelected(item);
                if (isDoubleCheck) {

                    if (listBean != null) {
                        mSelectedItemList.remove(listBean);
                    } else {
                        listBean = item;
                        mSelectedItemList.add(item);
                    }
                    notifyDataSetChanged();
                } else {
                    if (listBean != null) {
                        mSelectedItemList.remove(listBean);
                    } else {
                        mSelectedItemList.clear();
                        listBean = item;
                        mSelectedItemList.add(item);
                    }
                    notifyDataSetChanged();
                }

                if (mListener != null) {
                    mListener.onItemSelected(listBean);
                }
            }
        });

    }

    public void setDoubleCheck(boolean doubleCheck) {
        isDoubleCheck = doubleCheck;
    }

    private TicketEntity checkTicketIsSelected(TicketEntity ticket) {
        for (TicketEntity bean : mSelectedItemList) {
            if (bean.getTicketId().equals(ticket.getTicketId())) {
                return bean;
            }
        }
        return null;
    }

    public void clearCheckStatus() {
        mSelectedItemList.clear();
    }

    public void setSelectedItemList(List<TicketEntity> mSelectedItemList) {
        this.mSelectedItemList = mSelectedItemList;
        notifyDataSetChanged();
    }


    public interface OnItemSelectedListener {
        void onItemSelected(TicketEntity item);
    }

    private OnItemSelectedListener mListener;

    public void setOnItemSelectedListener(OnItemSelectedListener mListener) {
        this.mListener = mListener;
    }

}
